<?php
include_once("genser.php");

$gs = new genser($settings["main"]["stbd_server"], "2244");

switch($_POST["cmd"]) {
case "add":
	$pm->init($_POST["detail"], "|");

	// add new task
	$data = "cmd=task_add\n";
	$network->send($data);
	while($from = $network->recv()) {
		if(strpos($from, "task_add_reply") !== false) {
			break;
		}
	}
	$pm->init($from, "|");
	$task_id = $pm->get("id");

	// set details of new task
	$network->send("cmd=task_set|id=$task_id|data=todo|value=adc\n");
	$network->send("cmd=task_set|id=$task_id|data=todo_count|value=1\n");
	$network->send("cmd=task_set|id=$task_id|data=block_size|value=1\n");
	$network->send("cmd=task_set|id=$task_id|data=timeout|value=120\n");
	$network->send("cmd=task_set|id=$task_id|data=db_host|value=". $settings["default"]["db_host"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_port|value=". $settings["default"]["db_port"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_name|value=". $settings["default"]["db_name"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_user|value=". $settings["default"]["db_user"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_pass|value=". $settings["default"]["db_pass"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=days|value=". $settings["adc"]["days"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=start_node|value=". $settings["adc"]["start_node"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=stale_dn|value=". $settings["adc"]["stale_dn"] ."\n");

	// parameter is different if this is for stale or retired OUs
	if(!strcmp($_POST["detail"], "stale")) {
		$network->send("cmd=task_set|id=$task_id|data=action|value=load_stale\n");
		$network->send("cmd=task_set|id=$task_id|data=style|value=stale\n");
	}
	else {
		$network->send("cmd=task_set|id=$task_id|data=action|value=load_retired\n");
		$network->send("cmd=task_set|id=$task_id|data=style|value=retired\n");
	}

	// start task
	$network->send("cmd=task_start|id=$task_id\n");
	break;
case "daily":
	$pm->init($_POST["detail"], "|");

	// figure out number of rows in table
	$query = "SELECT * FROM n_adc_". $pm->get("id");
	$result = mysql_query($query);
	$num_rows = mysql_num_rows($result);

	// obtain track_id and increase
	$query = "SELECT * FROM n_adc WHERE id=" . $pm->get("id");
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$track_id = $row["track_id"] + 1;
	// save new track_id
	$query = "UPDATE n_adc SET track_id=$track_id WHERE id=" . $pm->get("id");
	$result = mysql_query($query);

	// add new task
	$data = "cmd=task_add\n";
	$network->send($data);
	while($from = $network->recv()) {
		if(strpos($from, "task_add_reply") !== false) {
			break;
		}
	}
	$pm->init($from, "|");
	$task_id = $pm->get("id");

	$pm->init($_POST["detail"], "|");
	$id = $pm->get("id");

	// set details of new task
	$network->send("cmd=task_set|id=$task_id|data=todo|value=adc\n");
	$network->send("cmd=task_set|id=$task_id|data=todo_count|value=$num_rows\n");
	$network->send("cmd=task_set|id=$task_id|data=block_size|value=100\n");
	$network->send("cmd=task_set|id=$task_id|data=timeout|value=30\n");
	$network->send("cmd=task_set|id=$task_id|data=db_host|value="
			. $settings["default"]["db_host"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_port|value="
			. $settings["default"]["db_port"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_name|value="
			. $settings["default"]["db_name"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_user|value="
			. $settings["default"]["db_user"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_pass|value="
			. $settings["default"]["db_pass"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=action|value=daily\n");
	$network->send("cmd=task_set|id=$task_id|data=instance|value=$id\n");

	// start task
	$network->send("cmd=task_start|id=$task_id\n");
	break;
case "next_state":
	$pm->init($_POST["detail"], "|");
	$id = $pm->get("id");
	$state = $pm->get("state") + 1;

	$query = "UPDATE n_adc SET state=$state WHERE id=$id";
	mysql_query($query);
	break;
case "advance":
	$state = $_POST["detail"];
	switch($state) {
		case 0:	// Advance to state = Pending
			$retired_id = false;
			$stale_id = false;

			// -------------------------
			// Submit Task - add_retired
			// -------------------------
			// obtain a new task_id
			$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet><cmd>task_add</cmd></packet>\r\n";
			?><?php
			$packet = simplexml_load_string( $gs->ask($query) );
			if($packet) {
				//echo "new task id: ".  $packet->id ."<br>\n";
				$detail = array(
						"todo" => "adc",
						"todo_count" => "1",
						"block_size" => "1",
						"timeout" => "120",
						"db_host" => $settings["default"]["db_host"],
						"db_port" => $settings["default"]["db_port"],
						"db_name" => $settings["default"]["db_name"],
						"db_user" => $settings["default"]["db_user"],
						"db_pass" => $settings["default"]["db_pass"],
						"days" => $settings["adc"]["days"],
						"start_node" => $settings["adc"]["start_node"],
						"stale_dn" => $settings["adc"]["stale_dn"],
						"action" => "load_stale",
						"style" => "stale"
					);
				// set the data value pairs of the task
				foreach($detail as $key => $value) {
					$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet>"
							."<cmd>task_detail_set</cmd>"
							."<id>".$packet->id ."</id>"
							."<data>$key</data>"
							."<value>$value</value>"
							."</packet>\r\n";
					$gs->ask($query);	// send to server
				}
				// remember the task id
				$retired_id = $packet->id;
			}

			// -----------------------
			// Submit Task - add_stale
			// -----------------------
			// obtain a new task_id
			$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet><cmd>task_add</cmd></packet>\r\n";
			$packet = simplexml_load_string( $gs->ask($query) );
			if($packet) {
				//echo "new task id: ". $packet->id ."<br>\n";
				$detail = array(
						"todo" => "adc",
						"todo_count" => "1",
						"block_size" => "1",
						"timeout" => "120",
						"db_host" => $settings["default"]["db_host"],
						"db_port" => $settings["default"]["db_port"],
						"db_name" => $settings["default"]["db_name"],
						"db_user" => $settings["default"]["db_user"],
						"db_pass" => $settings["default"]["db_pass"],
						"days" => $settings["adc"]["days"],
						"start_node" => $settings["adc"]["start_node"],
						"stale_dn" => $settings["adc"]["stale_dn"],
						"action" => "load_retired",
						"style" => "retired"
					);
				// set the data value pairs of the task
				foreach($detail as $key => $value) {
					$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet>"
							."<cmd>task_detail_set</cmd>"
							."<id>". $packet->id ."</id>"
							."<data>$key</data>"
							."<value>$value</value>"
							."</packet>\r\n";
					$gs->ask($query);	// send to server
				}
				// remember the task id
				$stale_id = $packet->id;
			}

			// start the tasks
			if($retired_id) {
				// start the task
				$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet>"
						."<cmd>task_start</cmd>"
						."<id>". $retired_id ."</id>"
						."</packet>\r\n";
				$gs->ask($query);	// send to server
			}
			if($stale_id) {
				// start the task
				$query = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><packet>"
						."<cmd>task_start</cmd>"
						."<id>". $stale_id ."</id>"
						."</packet>\r\n";
				$gs->ask($query);	// send to server
			}

			// State by default will be = '0', pending
			break;
		case 1:	// Advance to state = Move
			// Advance to next state
			advance_state_in_db($state);
			break;
		case 2:	// Advance to state = Active
			// Advance to next state
			advance_state_in_db($state);
			break;
		case 3:	// Advance to state = Complete
			// Advance to next state
			advance_state_in_db($state);
			break;
	}
	break;
case "set_StaleDN":
	$stale_dn = $_POST["detail"];
//	echo "set_StaleDN = '". $stale_dn ."'<br>\n";

	// Identifiy active records
	$query = "SELECT * FROM n_adc WHERE state != 3";
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result)) {
//		echo "record id = '". $row["id"] ."'<br>\n";

		// Set StaleDN for each active record
		$query = "UPDATE n_adc SET stale_dn='". $stale_dn ."' WHERE id=". $row["id"];
		mysql_query($query);
	}
	break;
}

// Advance all records which are not STATE = COMPLETE
function advance_state_in_db($state) {
	// Identifiy active records
	$query = "SELECT * FROM n_adc WHERE state != 3";
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result)) {
//		echo "record id = '". $row["id"] ."'<br>\n";

		// Advance each record one by one
		$query = "UPDATE n_adc SET state=$state WHERE id=". $row["id"];
		mysql_query($query);
	}
}
?>
